from django.views import View

from Venue.models import Venue, Vip, VenueSportYard
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequired


class VipView(View):
    """ 会员卡 """

    def get(self, request):
        """ 查询 """
        venue = Venue.objects.get(id=1)
        vips = Vip.objects.filter(venue=venue)
        lists = []
        for vip in vips:
            lists.append(vip.Info())
        return Res.Json(Res.OK, result=lists)

    @LoginRequired.Venue
    @Req.getJson
    def post(self, request):
        """ 添加 """
        name = request.json_dict.get("name")
        yardId = request.json_dict.get("yardId")
        suit = request.json_dict.get("suit")
        duration = request.json_dict.get("duration")
        rule = request.json_dict.get("rule", 0)
        message = request.json_dict.get("message", 0)
        price = request.json_dict.get("price", 0)
        value = request.json_dict.get("value", 0)

        venue = request.user.venue
        yard = VenueSportYard.objects.filter(venueSport__venue=venue, id=yardId).first()
        if not yard:
            return Res.Json(Res.ParamErr)

        vip = Vip.create(name=name, venue=venue, yard=yard, suit=suit, duration=duration,
                         rule=rule, message=message, price=price, value=value)
        return Res.Json(Res.OK, result=vip.Info())

    @LoginRequired.Venue
    @Req.getJson
    def put(self, request):
        """ 修改 """
        id = request.json_dict.get("id")
        name = request.json_dict.get("name")
        yardId = request.json_dict.get("yardId")
        suit = request.json_dict.get("suit")
        duration = request.json_dict.get("duration")
        rule = request.json_dict.get("rule", 0)
        message = request.json_dict.get("message", 0)
        price = request.json_dict.get("price", 0)
        value = request.json_dict.get("value", 0)

        venue = request.user.venue
        vip = Vip.objects.filter(venue=venue, id=id).first()
        if not vip:
            return Res.Json(Res.ParamErr)

        if name:
            vip.name = name
        if yardId:
            yard = VenueSportYard.objects.filter(venueSport__venue=venue, id=yardId).first()
            if not yard:
                return Res.Json(Res.ParamErr)
            vip.venueYard = yard
        if suit and suit in Vip.Suit.list():
            vip.suit = suit
        if duration:
            vip.duration = duration
        if rule:
            vip.rule = rule
        if message:
            vip.message = message
        if price:
            vip.price = price
        if value:
            vip.value = value

        vip.save()
        return Res.Json(Res.OK, result=vip.Info())

    @LoginRequired.Venue
    @Req.getJson
    def delete(self, request):
        """ 删除 """
        id = request.json_dict.get("id")

        venue = request.user.venue

        vip = Vip.objects.filter(venue=venue, id=id).first()
        if not vip:
            return Res.Json(Res.ParamErr)

        vip.delete()
        return Res.Json(Res.OK)
